﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ScarecrowLibrary.Network;

namespace Scarecrow_Network_Manager
{
    static class Global
    {
        #region Properties
        public static string ProductName
        {
            get { return "Scarecrow Network Manager"; }
        }
        public static string PublisherName
        {
            get { return "Scarecrow Studios"; }
        }

        public static string ProfilesPath
        {
            get { return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Scarecrow Studios\\Scarecrow Network Manager\\Profiles"; }
        }
        
        public static string ImagesPath
        {
            get { return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Scarecrow Studios\\Scarecrow Network Manager\\Images"; }
        }

        public static void NotifyCrash(Exception exc)
        {
            frmErrorReporter window = new frmErrorReporter(exc);
            window.ShowDialog();
        }
        public static scNetworkAdapter[] Nics
        { get; set; }
        #endregion

        #region Fields
        
        #endregion

        #region Methods
        public static scNetworkProfile GetCheckProfile(scNetworkProfile np)
        {
            scNetworkProfile check = null;
            try
            {
                check = new scNetworkProfile();
                check.Name = np.Name;
                check.ImagePath = np.ImagePath;

                //Aggiungo tutte le schede presenti e le loro impostazioni
                foreach (var nic in Global.Nics)
                {
                    check.AddAdapterProfile(nic.ExportToProfile());
                }

                //Aggiungo ogni scheda attualmente non presente, ma contenuta nel profilo
                scAdapterProfile[] checkProfiles = check.GetProfiles();
                scAdapterProfile[] profiles = np.GetProfiles();

                //Scorro i profili delle schede
                for (int i = 0; i < profiles.Length; i++)
                {
                    //Scorro le schede collegate
                    bool match = false;
                    for (int j = 0; j < checkProfiles.Length; j++)
                    {
                        //se il profilo contiene una scheda che non è presente tra quelle collegate, la aggiungo
                        if (profiles[i].NetworkAdapter == checkProfiles[j].NetworkAdapter)
                        {
                            match = true;
                            break;
                        }
                    }
                    //se non l'ho trovata, la aggiungo
                    if (!match)
                        check.AddAdapterProfile(profiles[i]);
                }
            }
            catch (Exception exc)
            {
                Global.NotifyCrash(exc);
            }
            return check;
        }
        public static Image LoadImageFromPath(string path)
        {
            Image img = new Image();
            BitmapImage src = new BitmapImage();
            src.BeginInit();
            src.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
            src.CacheOption = BitmapCacheOption.OnLoad;
            src.EndInit();
            img.Source = src;
            int q = src.PixelHeight;
            return img;
        }
        public static Image LoadImageFromResource(string relativePath)
        {
            Image img = new Image();
            BitmapImage src = new BitmapImage();
            src.BeginInit();
            src.UriSource = new Uri("pack://application:,,/" + relativePath, UriKind.RelativeOrAbsolute);
            src.CacheOption = BitmapCacheOption.OnLoad;
            src.EndInit();
            img.Source = src;
            int q = src.PixelHeight;
            return img;
        }

        public static ImageSource ResizeImage(ImageSource source, int width, int height)
        {
            Rect rect = new Rect(0, 0, width, height);

            DrawingVisual drawingVisual = new DrawingVisual();
            using (DrawingContext drawingContext = drawingVisual.RenderOpen())
            {
                drawingContext.DrawImage(source, rect);
            }

            RenderTargetBitmap resizeImage = new RenderTargetBitmap(
                (int)rect.Width, (int)rect.Height,
                96, 96,
                PixelFormats.Default);
            resizeImage.Render(drawingVisual);

            return resizeImage;
        }
        #endregion
    }
}
